﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using Microsoft.Win32;
using ActiproSoftware.Windows.Controls;
using ActiproSoftware.Windows.Controls.Ribbon;
using ActiproSoftware.Windows.Controls.Ribbon.Input;
using ActiproSoftware.Windows.Controls.Ribbon.Themes;
using ActiproSoftware.Windows.Controls.Ribbon.UI;
using RibbonControls = ActiproSoftware.Windows.Controls.Ribbon.Controls;
using ActiproSoftware.Windows.DocumentManagement;
using ActiproSoftware.Windows.Media;
using ActiproSoftware.Windows.Themes;
using XWord.GUI.Views.Tabs;
using ActiproSoftware.Windows.Controls.Ribbon.Controls;
using ActiproSoftware.Windows.Controls.Docking.Themes;
namespace XWord.GUI.Views.Fundamental
{
    /// <summary>
    /// Interaction logic for WordTabView.xaml
    /// </summary>
    public partial class ThemeTabView : Tab
    {
        internal const string CustomThemeDarkRed = "DarkRed";
        internal const string CustomThemeGoldenrod = "Goldenrod";
        internal const string CustomThemeGreen = "Green";
        internal const string CustomThemePurple = "Purple";
        internal const string CustomThemeSteelBlue = "SteelBlue";
        internal const string CustomThemeTan = "Tan";

        public ThemeTabView()
        {
            InitializeComponent();
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ThemeBlack, themeChangeCommand_Execute, themeChangeCommand_CanExecute));
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ThemeBlue, themeChangeCommand_Execute, themeChangeCommand_CanExecute));
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ThemeCustom, themeChangeCommand_Execute, themeChangeCommand_CanExecute));
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ThemeSilver, themeChangeCommand_Execute, themeChangeCommand_CanExecute));
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ThemeSystem, themeChangeCommand_Execute, themeChangeCommand_CanExecute));
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ToggleGlassEnabled, toggleGlassEnabledCommand_Execute, toggleGlassEnabledCommand_CanExecute));
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ToggleMaximizeButtonVisible, toggleMaximizeButtonVisibleCommand_Execute, toggleMaximizeButtonVisibleCommand_CanExecute));
            this.CommandBindings.Add(new CommandBinding(ThemeApplicationCommands.ToggleMinimizeButtonVisible, toggleMinimizeButtonVisibleCommand_Execute, toggleMinimizeButtonVisibleCommand_CanExecute));
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> needs to determine whether it can execute.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="CanExecuteRoutedEventArgs"/> that contains the event data.</param>
        private void themeChangeCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            // Always allow execution of the command
            e.Handled = true;
            e.CanExecute = true;

            // Get the current theme name
            string currentTheme = ThemeManager.GetTheme(this);

            ICheckableCommandParameter parameter = e.Parameter as ICheckableCommandParameter;
            if (parameter != null)
            {
                parameter.Handled = true;
                if (e.Command == ThemeApplicationCommands.ThemeBlack)
                    parameter.IsChecked = (currentTheme == CommonThemeName.Office2010Black.ToString());
                else if (e.Command == ThemeApplicationCommands.ThemeBlue)
                    parameter.IsChecked = (currentTheme == CommonThemeName.Office2010Blue.ToString());
                else if (e.Command == ThemeApplicationCommands.ThemeSilver)
                    parameter.IsChecked = (currentTheme == CommonThemeName.Office2010Silver.ToString());
                else if (e.Command == ThemeApplicationCommands.ThemeSystem)
                    parameter.IsChecked = (currentTheme == CommonThemeName.Generic.ToString());
                else
                    parameter.IsChecked = (currentTheme == parameter.Tag as string);
            }
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> is executed.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">An <see cref="ExecutedRoutedEventArgs"/> that contains the event data.</param>
        private void themeChangeCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            ICheckableCommandParameter parameter = e.Parameter as ICheckableCommandParameter;
            ColorConverter converter = new ColorConverter();
            
            // NOTE: Normally instead of calling ThemeManager.SetTheme like we do below, you would simply call 
            //   ThemeManager.CurrentTheme = "themename" since that changes the theme for the entire application...
            //   Here we are simply changing the theme for this sample
            var target = Application.Current.MainWindow;
            if (e.Command == ThemeApplicationCommands.ThemeBlack)
                ThemeManager.SetTheme(target, CommonThemeName.Office2010Black.ToString());
            else if (e.Command == ThemeApplicationCommands.ThemeBlue)
                ThemeManager.SetTheme(target, CommonThemeName.Office2010Blue.ToString());
            else if (e.Command == ThemeApplicationCommands.ThemeSilver)
                ThemeManager.SetTheme(target, CommonThemeName.Office2010Silver.ToString());
            else if (e.Command == ThemeApplicationCommands.ThemeSystem)
                ThemeManager.SetTheme(target, CommonThemeName.Generic.ToString());
            else
            {
                // Unregister any custom themes that have already been loaded to save on memory
                ThemeManager.Unregister(CommonThemeName.Custom.ToString());
                ThemeManager.Unregister(CustomThemeDarkRed);
                ThemeManager.Unregister(CustomThemeGoldenrod);
                ThemeManager.Unregister(CustomThemeGreen);
                ThemeManager.Unregister(CustomThemePurple);
                ThemeManager.Unregister(CustomThemeSteelBlue);
                ThemeManager.Unregister(CustomThemeTan);

                
                SetCustomTheme(parameter.Tag.ToString(), (Color)converter.ConvertFrom(parameter.Tag));
                //switch (((CheckableCommandParameter)e.Parameter).Tag.ToString())
                //{
                //    case CustomThemeDarkRed:
                        
                //        break;
                //    case CustomThemeGoldenrod:
                //        SetCustomTheme(CustomThemeGoldenrod, Colors.Goldenrod);
                //        break;
                //    case CustomThemeGreen:
                //        SetCustomTheme(CustomThemeGreen, Colors.Green);
                //        break;
                //    case CustomThemePurple:
                //        SetCustomTheme(CustomThemePurple, Colors.Purple);
                //        break;
                //    case CustomThemeSteelBlue:
                //        SetCustomTheme(CustomThemeSteelBlue, Colors.SteelBlue);
                //        break;
                //    case CustomThemeTan:
                //        SetCustomTheme(CustomThemeTan, Colors.Tan);
                //        break;
                //}
            }
        }

        private void SetCustomTheme(string name, Color color)
        {
            var target = Application.Current.MainWindow;
            ThemeManager.Register(name, RibbonBaseDictionary.GroupName,
                          new TintedResourceDictionary(RibbonOffice2007BlueDictionary.Instance,
                              color, RibbonCommonDictionary.DefaultUntintedGroups));
            ThemeManager.Register(name, DockingBaseDictionary.GroupName,
                new TintedResourceDictionary(DockingOffice2007BlueDictionary.Instance,
                    color, RibbonCommonDictionary.DefaultUntintedGroups));
            ThemeManager.SetTheme(target, name);
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> needs to determine whether it can execute.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="CanExecuteRoutedEventArgs"/> that contains the event data.</param>
        private void toggleGlassEnabledCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //e.CanExecute = ribbon.IsHostedInRibbonWindow;
            //e.Handled = true;

            //ICheckableCommandParameter parameter = e.Parameter as ICheckableCommandParameter;
            //if (parameter != null)
            //{
            //    parameter.Handled = true;
            //    parameter.IsChecked = this.IsGlassEnabled;
            //}
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> is executed.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">An <see cref="ExecutedRoutedEventArgs"/> that contains the event data.</param>
        private void toggleGlassEnabledCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            //this.IsGlassEnabled = !this.IsGlassEnabled;
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> needs to determine whether it can execute.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="CanExecuteRoutedEventArgs"/> that contains the event data.</param>
        private void toggleMaximizeButtonVisibleCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //e.CanExecute = ribbon.IsHostedInRibbonWindow;
            //e.Handled = true;

            //ICheckableCommandParameter parameter = e.Parameter as ICheckableCommandParameter;
            //if (parameter != null)
            //{
            //    parameter.Handled = true;
            //    parameter.IsChecked = this.IsMaximizeButtonVisible;
            //}
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> is executed.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">An <see cref="ExecutedRoutedEventArgs"/> that contains the event data.</param>
        private void toggleMaximizeButtonVisibleCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
          //  this.IsMaximizeButtonVisible = !this.IsMaximizeButtonVisible;
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> needs to determine whether it can execute.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">A <see cref="CanExecuteRoutedEventArgs"/> that contains the event data.</param>
        private void toggleMinimizeButtonVisibleCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //e.CanExecute = ribbon.IsHostedInRibbonWindow;
            //e.Handled = true;

            //ICheckableCommandParameter parameter = e.Parameter as ICheckableCommandParameter;
            //if (parameter != null)
            //{
            //    parameter.Handled = true;
            //    parameter.IsChecked = this.IsMinimizeButtonVisible;
            //}
        }

        /// <summary>
        /// Occurs when the <see cref="RoutedCommand"/> is executed.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">An <see cref="ExecutedRoutedEventArgs"/> that contains the event data.</param>
        private void toggleMinimizeButtonVisibleCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
          //  this.IsMinimizeButtonVisible = !this.IsMinimizeButtonVisible;
        }

    }
}
